package com.wedo.mall.activity.comment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.widget.EHackyViewPager;
import com.wedo.mall.R;
import com.wedo.mall.fragment.ImageFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnPageChange;

/**
 * Created by e on 2017/10/15.
 */
public class ImageActivity extends EActivity {

    private static final String KEY_POSITION = "KEY_POSITION";
    private static final String KEY_IMAGE_URLS = "KEY_IMAGE_URLS";

    @BindView(R.id.hvp_images)      EHackyViewPager hvp_images;
    @BindView(R.id.tv_indicator)    TextView tv_indicator;

    private int mImagesSize;

    public static void image(Context context, String url) {
        if (TextUtils.isEmpty(url)){
            return;
        }
        ArrayList<String> images = new ArrayList<>();
        images.add(url);
        images(context,images,0);
    }

    public static void images(Context context, ArrayList<String> images, int position) {
        if (images == null || images.isEmpty()) {
            return;
        }
        Bundle extras = new Bundle();
        extras.putStringArrayList(KEY_IMAGE_URLS, images);
        extras.putInt(KEY_POSITION, position);

        Intent intent = new Intent(context, ImageActivity.class);
        intent.putExtras(extras);
        context.startActivity(intent);
    }

    @Override
    protected int layout() {
        return R.layout.activity_images;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        disableSwipeBack();
        Bundle bundle = getIntent().getExtras();

        int position = bundle.getInt(KEY_POSITION,0);
        ArrayList<String> images = bundle.getStringArrayList(KEY_IMAGE_URLS);
        mImagesSize = images.size();

        ImagePagerAdapter adapter = new ImagePagerAdapter(getSupportFragmentManager(),images);
        hvp_images.setAdapter(adapter);
        hvp_images.setCurrentItem(position);
        onImageSelected(position);
    }

    @OnPageChange(R.id.hvp_images)
    public void onImageSelected(int position){
        if (mImagesSize == 1) {
            tv_indicator.setVisibility(View.GONE);
            return;
        }
        position+=1;
        tv_indicator.setText(String.format("%s/%s", position, mImagesSize));
    }

    private static class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public ArrayList<String> images;
        public ImagePagerAdapter(FragmentManager fm, ArrayList<String> images) {
            super(fm);
            this.images = images;
        }

        @Override
        public int getCount() {
            return images == null ? 0 : images.size();
        }

        @Override
        public Fragment getItem(int position) {
            String url = images.get(position);
            return ImageFragment.with(url);
        }
    }
}
